﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Net;

namespace SapTestConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create();
            //GetCarrierByID();
            //GetCarrierByID();
            CreateSubscription("");
        }
        private static void GetCarrierByID()
        {
            /******************************************************************************
            * Connection to the service
            ******************************************************************************/
            string serviceUrl = "http://p138.coil.sap.com:8080/sap/opu/sdata/sap/ZCRM_LEAD";
            ZCRM_LEAD.ZCRM_LEAD service = new ZCRM_LEAD.ZCRM_LEAD(new Uri(serviceUrl));
            service.Credentials = new NetworkCredential("proen", "invent1");
            /******************************************************************************
            * Execute the Query to the service to retrieve the carrier
            ******************************************************************************/
            var coll = (from ZCRM_LEAD.zxact_lead l in service.zxact_leadCollection
                        where (l.value == "0050560126391ED0BFF9493071CFBFDD" && l.scheme_id == "ZXACT_LEAD"
                        && l.scheme_agency_id == "CR7_800")
                           select l).FirstOrDefault();
            /******************************************************************************
            * Using the returned value
            ******************************************************************************/
            
            
        }

        private static void Create()
        {
            string serviceUrl = "http://p138.coil.sap.com:8080/sap/opu/sdata/sap/ZCRM_LEAD";
            ZCRM_LEAD.ZCRM_LEAD service = new ZCRM_LEAD.ZCRM_LEAD(new Uri(serviceUrl));
            service.Credentials = new NetworkCredential("proen", "invent1");

            ZCRM_LEAD.zxact_lead l = new ZCRM_LEAD.zxact_lead
            {
                description = "TEST unique",
                long_text = "TEST",
                scheme_agency_id = "",
                scheme_id = "",
                value = ""
            };

            service.AddTozxact_leadCollection(l);

            try
            {
                var res = service.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }


        }


        public static void CreateSubscription(string guid)
        {
            string serviceUrl = "http://p138.coil.sap.com:8080/sap/opu/sdata/sap/ZCRM_LEAD";
            ZCRM_LEAD.ZCRM_LEAD service = new ZCRM_LEAD.ZCRM_LEAD(new Uri(serviceUrl));
            service.Credentials = new NetworkCredential("proen", "invent1");

            ZCRM_LEAD.Subscription s = new ZCRM_LEAD.Subscription
            {
                deliveryAddress = "http://lehavi.com:2057/Sap/LeadUpdated",
                scheme_agency_id = "",
                scheme_id = "",
                value = "",
                collection = "zxact_leadCollection",
                filter = string.Format("value eq '{0}' and scheme_id eq '{1}' and scheme_agency_id eq '{2}'", guid, "ZXACT_LEAD", "CR7_800"),
                
            };

            service.AddToSubscriptionCollection(s);
            
            try
            {
                var res = service.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

    }
}
